/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Lvod.PlayList;

import Lvod.Config.ConfigLoader;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author wcss
 */
public class PlayListLoader {

    public static wox.serial.Easy quickEasy = new wox.serial.Easy();
    public static ArrayList playLists = new ArrayList();

    /**
     * 获取默认路径
     *
     * @return
     */
    public static String getPlayListUrlStr() {
        File ff = new File(JAppToolKit.JRunHelper.getUserHomeDirPath() + "/.qvodcache/");
        if (!ff.exists()) {
            ff.mkdirs();
        }
        return JAppToolKit.JRunHelper.getUserHomeDirPath() + "/.qvodcache/" + ConfigLoader.config.playListName;
    }

    /**
     * 获取需要下载的列表
     *
     * @return
     */
    public static ArrayList<HistoryItem> getNeedDownloadList() 
    {
        ArrayList<HistoryItem> results = new ArrayList<HistoryItem>();

        for(Object obj : playLists)
        {
            HistoryItem hi = (HistoryItem)obj;
            File ff = new File(hi.getDownloadMoviePath());
            if (!ff.exists())
            {
                results.add(hi);                
            }
        }
        
        return results;
    }

    /**
     * 查找历史记录对象通过下载地址
     *
     * @param downloadUrl
     * @return
     */
    public static HistoryItem findHistory(String downloadUrl) {
        if (playLists != null) {
            for (Object obj : playLists) {
                HistoryItem hi = (HistoryItem) obj;
                if (hi.downloadUrl != null && hi.downloadUrl.equals(downloadUrl)) {
                    return hi;
                }
            }
        }
        return null;
    }

    /**
     * 增加下载历史记录
     *
     * @param downloadUrl
     * @param bufferDir
     * @param bufferSaveName
     * @param localSaveName
     * @return
     */
    public static Boolean addHistory(String downloadUrl, String bufferDir, String bufferSaveName, String localSaveName) {
        HistoryItem hi = findHistory(downloadUrl);
        if (hi != null) {
            return false;
        } else {
            HistoryItem hin = new HistoryItem();
            hin.downloadUrl = downloadUrl;
            hin.bufferDir = bufferDir;
            hin.bufferSaveName = bufferSaveName;
            hin.startTime = new Date().getTime();
            hin.downloadDir = ConfigLoader.config.downloadDir;
            hin.localSaveName = localSaveName;
            playLists.add(hin);
            return true;
        }
    }

    /**
     * 载入播放列表(新XML方式)
     */
    public static void loadPlayListWithXML() {
        File playlist = new File(getPlayListUrlStr());
        if (playlist.exists()) {
            try {
                ArrayList temp = (ArrayList) quickEasy.loadfromfile(playlist.getAbsolutePath());
                playLists.clear();
                playLists.addAll(temp);
            } catch (Exception ex) {
                Logger.getLogger(PlayListLoader.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    /**
     * 保存播放列表(新XML方式)
     */
    public static void savePlayListWithXML() {
        if (playLists != null) {
            try {
                quickEasy.savetofile(playLists, getPlayListUrlStr());
            } catch (Exception ex) {
                Logger.getLogger(PlayListLoader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * 载入播放列表(新旧兼容方式)
     */
    public static void loadPlayList() throws Exception {
        File playli = new File(getPlayListUrlStr());
        if (playli.exists()) {
            playLists.clear();
            ArrayList<String> datas = readOldPlayList();
            if (datas != null && datas.size() > 0) {
                String first = datas.get(0) + "" + ((datas.size() >= 2) ? datas.get(1) : "");
                if (first.contains("<object")) {
                    //XML方式
                    loadPlayListWithXML();
                } else {
                    //旧的方式
                    for (String str : datas) {
                        String movieName = VodUrlFilters.getMovieStr(str);
                        addHistory(str, "", "", VodUrlFilters.decodeMovieName(movieName));
                    }
                    savePlayListWithXML();
                }
            }
        }
    }

    /**
     * 载入播放列表(旧)
     */
    public static ArrayList<String> readOldPlayList() throws Exception {
        ArrayList<String> oldlist = new ArrayList<String>();
        File configfile = new File(getPlayListUrlStr());
        if (configfile.exists()) {
            String[] team = JAppToolKit.JDataHelper.readAllLines(configfile.getAbsolutePath());
            for (String str : team) {
                oldlist.add(str.trim());
            }
        }
        return oldlist;
    }
}
